package org.xncj.Controller;

import org.constant.MessageConstant;
import org.entity.PageResult;
import org.entity.QueryPageBean;
import org.entity.Result;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.utils.QiniuUtils;
import org.xncj.CheckGroup;
import org.xncj.CheckItem;
import org.xncj.SetMealService;
import org.xncj.Setmeal;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.List;
import java.util.UUID;

@RestController
@RequestMapping("/setmeal")
public class SetMealControll {
    @Resource
    SetMealService setMealService;

    @PostMapping("/upload")
    public Result upload(MultipartFile imgFile)throws IOException {
        //从容器中获取线程池,再从线程池去执行任务
        try {
            //1.上传图片的名称
            String path = imgFile.getOriginalFilename();
            path = UUID.randomUUID().toString() + path;
            byte[] bytes = imgFile.getBytes();
            //2.使用工具类完成上传
            QiniuUtils.upload2Qiniu(bytes, path);
            return new Result(true,MessageConstant.PIC_UPLOAD_SUCCESS,path);
        }catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.PIC_UPLOAD_FAIL);
        }
    }
    @PostMapping("/addSetMeal/{checkgroupIds}")
    public Result addSetMeal(@PathVariable("checkgroupIds") Integer[] checkgroupIds, @RequestBody Setmeal setmeal) {
        try {
            setMealService.addSetMeal(checkgroupIds,setmeal);
            return new Result(true,MessageConstant.ADD_SETMEAL_SUCCESS);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false,MessageConstant.ADD_SETMEAL_FAIL);
        }
    }

    @GetMapping("/findAll")
    public Result findAll(){
        List<Setmeal> setmeals =  null;
        try {
            setmeals = setMealService.findAll();
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false,MessageConstant.GET_MEMBER_NUMBER_REPORT_SUCCESS);
        }
        return new Result(true,MessageConstant.GET_MEMBER_NUMBER_REPORT_FAIL,setmeals);
    }

    @RequestMapping("/findAllsetmeal")
    public PageResult findAllsetmeal(@RequestBody QueryPageBean queryPageBean) {
        PageResult pageResult = setMealService.findAllsetmeal(queryPageBean);
        return pageResult;
    }

    @PostMapping("/updatesetmeal/{id}")
    public Result updatesetmeal(@PathVariable("id") Integer setmealId) {
        try {
            setMealService.updatesetmeal(setmealId);
            return new Result(true,MessageConstant.DELETE_MEMBER_SUCCESS);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false,MessageConstant.DELETE_MEMBER_FAIL);
        }
    }

    @PostMapping("/Editsetmeal/{checkgroupIds}")
    public Result Editsetmeal(@PathVariable("checkgroupIds") Integer[] setmealId, @RequestBody Setmeal setmeal) {
        try {
            setMealService.Editsetmeal(setmealId, setmeal);
            return new Result(true, MessageConstant.EDIT_CHECKGROUP_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.EDIT_CHECKGROUP_FAIL);
        }
    }
}
